import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/Common/TextFieldItem.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/dropdownmenu.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//空托叠盘
class EmptyStacking extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _EmptyStacking();
  }
}

//ListWidget
class _EmptyStacking extends State<EmptyStacking> {
  bool load = true;
  List _data = new List();
  TextEditingController palletCode = TextEditingController();
  TextEditingController emptyPallet = TextEditingController();
  TextEditingController stack = TextEditingController();

  String selectValue;
  String selectItem;
  int selectIndex;


  @override
  void initState() {
    getTrayType();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "空托叠盘",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
            visibility: load,
            builder: Column(
              children: <Widget>[
                Expanded(
                    child: Container(
                        child: Scrollbar(
                  child: SingleChildScrollView(
                    padding: EdgeInsets.only(top: 20, left: 12, right: 12),
                    child: Column(children: <Widget>[
                      DropDownMenu(
                        value: selectValue,
                        hitText: "选择托盘类型",
                        datas: _data,
                        keyItem: {"text": "value"},
                        onChange: (data) {
                          setState(() {
                            selectValue = data["value"];
                            selectIndex=data["index"];
                          });
                        },
                      ),
                      TextFieldItem(hintText: "扫描或输入托盘码",textEditingController: palletCode,),
                      TextFieldItem(hintText: "空托位置",textEditingController: emptyPallet,),
                      TextFieldItem(hintText: "叠盘位置",textEditingController: stack,)
                    ]),
                  ),
                ))),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: FlatButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(Radius.circular(0))),
                        padding: EdgeInsets.all(15),
                        color: Color(0xFF42AFF0),
                        child: Text(
                          "完成",
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () {

                          if(selectValue!=null){
                            if(palletCode.text!=null&&palletCode.text.length>0){
                              if(emptyPallet.text!=null&&emptyPallet.text.length>0){
                                if(stack.text!=null&&stack.text.length>0){
                                  setState(() {
                                    load = true;
                                  });
                                   upData();
                                }else{
                                  TostUtil.showToast("请输入叠盘位置");
                                }
                              }else{
                                TostUtil.showToast("请输入空托位置");
                              }
                            }else{
                              TostUtil.showToast("扫描或输入托盘码");
                            }
                          }else{
                            TostUtil.showToast("请选择托盘类型");
                          }
                        },
                      ),
                    )
                  ],
                )
              ],
            )));
  }

  void getTrayType() {
    InternetAPI.getInstance().get("WorkTool/PrPallet/GetPalletTypeList",
        (data) {
      print(data);
      setState(() {
        load = false;
        _data = data;
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'operaterUserName': DataSingleton().Data.username,
      'tenantId': DataSingleton().Data.tenantId,
      'Warehouseid': DataSingleton().warehouseChoice.id,
      'dictgroup': '托盘类型',
      'isactive': true
    });
  }

  void upData() {
    InternetAPI.getInstance().post("WMS/MiWarehouseInOrder/SendPalletizing",
            (data) {
          print(data);
          if(data==true){
            setState(() {
              load = false;
              palletCode.text="";
              emptyPallet.text="";
              stack.text="";
            });
          }
        },errorCallBack: (err){
          setState(() {
            load = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'operaterUserName': DataSingleton().Data.username,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'pallettype': selectValue,
          'pallettypename': _data.elementAt(selectIndex)["text"],
          'palletcode': palletCode.text,
          'tolocationcode': emptyPallet.text,
          'fromlocationcode': stack.text,
        });
  }
}
